package com.cheboohui.iqs.deal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.menu.ErrorNews;
import com.cheboohui.iqs.spring.entity.price.request.CheckCar;
import com.cheboohui.iqs.spring.entity.price.request.ConfirmInsured;
import com.cheboohui.iqs.spring.entity.price.request.PayRequest;
import com.cheboohui.iqs.spring.entity.price.request.PriceRequest;
import com.cheboohui.iqs.spring.entity.price.response.CheckCarResult;
import com.cheboohui.iqs.spring.entity.price.response.ConfirmInsuredResult;
import com.cheboohui.iqs.spring.entity.price.response.PriceResult;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;
import com.cheboohui.iqs.taiping.direct.e.E05Request;
import com.cheboohui.iqs.taiping.direct.order.CarListGet;
import com.cheboohui.iqs.taiping.direct.order.ConfirmPolicy;
import com.cheboohui.iqs.taiping.direct.order.PayPolicy;
import com.cheboohui.iqs.taiping.direct.price.PriceTaiPing;

/**
 * 报价相关接口处理
 * 
 * @author xielonghai
 *
 */
public class PriceDeal {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(PriceDeal.class);

	/**
	 * 车险保费车辆查询
	 * 
	 * @param checkCar
	 * @return
	 */
	public static CheckCarResult checkCar(CheckCar checkCar) {
		log.info("进入" + Config.value + ",车险车辆查询接口...");
		try {
			// ************
			CheckCarResult result = null;
			ResTaiPing car = E05Request.requestE05("", checkCar.getLicense().getLabelNo());
			if (car.isState()) {
				JSONArray list = car.getResult().getJSONArray("vehicle");

				log.info(Config.value + ",车险车辆查询e05返回session:" + car.getHead().getString("session"));
				try {
					if (null == list || list.isEmpty() || list.size() < 1)
						result = new CheckCarResult(ErrorNews.C02, "返回车型列表为空");
					else
						result = new CheckCarResult(CarListGet.getCars(list));

				} catch (Exception e) {
					log.error(Config.value + ",车险车辆查询后处理返回结果出现异常:" + e);
					log.error(Config.value + "e05,接口返回:" + JSONObject.toJSONString(car));
					result = new CheckCarResult(ErrorNews.L03, "车险车辆查询后处理返回结果出现异常:" + e.getMessage());
				}
			} else {
				result = new CheckCarResult(car.getError(), car.getMsg());
			}
			// CheckCarResult result = new CheckCarResult(ErrorNews.L03,
			// "暂不提供该接口");
			log.info(Config.value + ",车险车辆查询接口查询结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",车险车辆查询出现异常:" + e);
			return new CheckCarResult(ErrorNews.L03, "车险车辆查询接口异常:" + e.getMessage());
		}

	}

	/**
	 * 车险保费计算
	 * 
	 * @param priceRequest
	 * @return
	 */
	public static PriceResult carPrice(PriceRequest priceRequest) {
		log.info("进入" + Config.value + ",车险报价接口...");
		try {
			PriceResult result = PriceTaiPing.price(priceRequest);
			// new PriceResult(ErrorNews.L03, "暂不提供该接口");
			log.info(Config.value + ",车险保费计算接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",车险报价出现异常" + e);
			return new PriceResult(ErrorNews.L03, "车险报价接口异常:" + e.getMessage());
		}

	}

	/**
	 * 车险确认投保
	 * 
	 * @param confirmInsured
	 * @return
	 */
	public static ConfirmInsuredResult confirm(ConfirmInsured confirmInsured) {
		log.info("进入" + Config.value + ",车险确认投保接口...");
		try {
			ConfirmInsuredResult result = ConfirmPolicy.confirm(confirmInsured);
			// new ConfirmInsuredResult(ErrorNews.L03, "暂不提供该接口");
			log.info(Config.value + ",车险确认投保接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",车险确认投保出现异常" + e);
			return new ConfirmInsuredResult(ErrorNews.L03, "车险确认投保接口异常:" + e.getMessage());
		}
	}

	/**
	 * 车险支付登记
	 * 
	 * @param payRequest
	 * @return
	 */
	public static ConfirmInsuredResult pay(PayRequest payRequest) {
		log.info("进入" + Config.value + ",车险支付登记接口...");
		try {
			ConfirmInsuredResult result = PayPolicy.pay(payRequest);
			// new ConfirmInsuredResult(ErrorNews.L03, "暂不提供该接口");
			log.info(Config.value + ",车险支付登记接口结果:" + JSONObject.toJSONString(result));
			return result;
		} catch (Exception e) {
			log.error(Config.value + ",车险支付登记出现异常" + e);
			return new ConfirmInsuredResult(ErrorNews.L03, "车险支付登记接口异常:" + e.getMessage());
		}
	}
}
